<template>
  <div class="columns py-6">
    <div class="column is-half is-offset-one-quarter">
      <!-- 卡片布局 -->
      <el-card shadow="never">
        <div slot="header" class="has-text-centered has-text-weight-bold">
          加入🐕家军
        </div>
        <div>
          <el-form
              ref="ruleForm"
              v-loading="loading"
              :model="ruleForm"
              status-icon
              :rules="rules"
              label-width="100px"
              class="demo-ruleForm"
          >
            <!-- 账号 -->
            <el-form-item label="账号" prop="name">
              <el-input v-model="ruleForm.name"/>
            </el-form-item>

            <!-- 密码 -->
            <el-form-item label="密码" prop="pass">
              <el-input
                  v-model="ruleForm.pass"
                  type="password"
                  autocomplete="off"
              />
            </el-form-item>

            <!-- 确认密码 -->
            <el-form-item label="确认密码" prop="checkPass">
              <el-input
                  v-model="ruleForm.checkPass"
                  type="password"
                  autocomplete="off"
              />
            </el-form-item>

            <!-- 邮箱 -->
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="ruleForm.email" autocomplete="off"/>
            </el-form-item>

            <el-form-item>
              <el-button
                  type="primary"
                  @click="submitForm('ruleForm')"
              >立即注册
              </el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import {userRegister} from '@/api/auth/auth'

export default {
  name: "Register",
  data() {
    // 密码验证
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码！'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      // 加载中
      loading: false,
      ruleForm: {
        name: '',
        pass: '',
        checkPass: '',
        email: ''
      },
      // 校验规则
      rules: {
        name: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {
            min: 2,
            max: 10,
            message: '长度在 2 到 10 个字符',
            trigger: 'blur'
          }
        ],
        pass: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {
            min: 6,
            max: 20,
            message: '长度在 6 到 20 个字符',
            trigger: 'blur'
          }
        ],
        checkPass: [
          {required: true, message: '请再次输入密码', trigger: 'blur'},
          {validator: validatePass, trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱地址', trigger: 'blur'},
          {
            type: 'email',
            message: '请输入正确的邮箱地址',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  methods: {
    // 表单提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          userRegister(this.ruleForm)
              .then((value) => {
                const {code, message} = value
                if (code === 200) {
                  this.$message({
                    message: '注册成功！恭喜您成为🐕家军的一员！',
                    type: 'success'
                  })
                  setTimeout(() => {
                    this.loading = false
                    // 100ms以后跳到登录页面
                    this.$router.push({path: this.redirect || '/login'})
                  }, 0.1 * 1000)
                } else {
                  this.$message.error('注册失败，' + message)
                }
              })
              .catch(() => {
                this.loading = false
              })
        } else {
          return false
        }
      })
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>

</style>